﻿Ext.define('app.system.area.Index',
{
    extend: 'Ext.panel.Panel',
    border: true,
    layout: 'border',
    initComponent: function () {
        var me = this;
        me.items = [
            me.getAreaTree(),
            me.getUserList()
        ];
        me.callParent(arguments);
    },
    getAreaTree: function () {
        var me = this;
        if (!this.areaTree) {
            this.areaTree = new Ext.tree.Panel({
                border: false,
                split: true,
                region: 'west',
                title: '区域管理',
                width: 500,
                //tbar: [
                //    "->",
                //    {
                //        icon: '/Content/images/page_save.png',
                //        text: '保存编辑',
                //        handler: function () {
                //            var arry = [];
                //            var store = me.areaTree.store;
                //            for (var i = 0; i < store.getCount() ; i++) {
                //                var record = store.getAt(i);
                //                if (record.data.id != "0") {
                //                    arry.push({ id: record.data.id, name: record.data.text, code: record.data.code, sort: record.data.sort });
                //                }
                //            }
                //            if (arry.length == 0) {
                //                Ext.Msg.alert('系统提示', "没有需要保存的数据");
                //                return false;
                //            }
                //            App.AjaxPost('/api/services/app/quan/EditQuan', arry, function (data) {
                //                store.load({
                //                    callback: function () {
                //                        me.areaTree.expandAll();
                //                    }
                //                });
                //            });
                //        }
                //    }
                //],
                viewConfig: {
                    plugins: [
                       //Ext.create('Ext.grid.plugin.CellEditing', { clicksToEdit: 1 }),
                       {
                           ptype: 'treeviewdragdrop'
                       }
                    ],
                    listeners: {
                        beforedrop: function (node, data, overModel, dropPostion, dropFunction, options) {

                            if (overModel.get("leaf")) {

                                overModel.set("leaf", false);

                            }

                        },

                        drop: function (node, data, overModel, dropPostion, options) {

                            //ajax的操作把数据同步到后台数据库
                            console.log("把" + data.records[0].get("text") + "移动到" + overModel.get("text"));
                            console.log(dropPostion);
                            me.move(data.records[0].get('id'), overModel.get('id'), dropPostion);
                        }
                    }
                },
                store: new Ext.data.TreeStore({
                    root: { expanded: true },
                    autoLoad: true,
                    nodeParam: 'parentId',
                    // 根节点的参数值是0
                    defaultRootId: "0",
                    proxy: {
                        type: 'ajax',
                        reader: {
                            type: 'json',
                            rootProperty: function (node) {
                                return node.result || node.children;
                            }
                        },
                        url: '/api/services/app/quan/GetQuanTree'
                    }
                }),
                root: { id: '0', text: '一级目录' },
                useArrows: true,
                rootVisible: true,
                listeners: {
                    itemclick: function (tree, record, item, index, e, eOpts) {
                        me.getUserList().store.filter = { quanId: record.data.id };
                        me.getUserList().store.load();
                    },
                    itemcontextmenu: function (menutree, record, item, index, e, eOpts) {
                        e.preventDefault();
                        e.stopEvent();
                        var nodemenu = new Ext.menu.Menu({
                            floating: true,
                            items: [
                            {
                                icon: '/Content/images/add.png',
                                text: "新增子项",
                                handler: function () {
                                    me.editArea(0, '', record.data.id);
                                }
                            },
                            {
                                icon: '/Content/images/edit.gif',
                                text: "编辑名称",
                                handler: function () {
                                    me.editArea(record.data.id, record.data.text, record.data.parentId);
                                }
                            },
                            {
                                icon: '/Content/images/delete.gif',
                                text: "删除区域",
                                handler: function () {
                                    Ext.Msg.confirm('系统提示', '确定要删除区域吗？', function (btn) {
                                        if (btn == 'yes') {
                                            App.AjaxRemove('/api/services/app/quan/RemoveQuan', { Id: record.data.id }, function (data) {
                                                me.areaTree.store.load({
                                                    callback: function () {
                                                        me.areaTree.expandAll();
                                                    }
                                                });
                                            });
                                        }
                                    });
                                }
                            },
                            ]
                        });
                        nodemenu.showAt(e.getXY());
                    },
                    afterrender: function (tree) { tree.expandAll(); }
                }
            });
        }
        return this.areaTree;
    },
    editArea: function (id, text, parentNodeId) {
        var me = this;
        var win = Ext.create("app.system.area.Edit", { parentNodeId: parentNodeId, nodeId: id, nodeText: text });
        win.on("savesuccess", function () {
            me.areaTree.store.load({
                callback: function () {
                    me.areaTree.expandAll();
                }
            });
            win.close();
        });
        win.show();
    },
    move: function (nodeId, overId, position) {
        //把XX移到UU
        //before||after||append
        var me = this;
        App.AjaxPost('/api/services/app/quan/MoveQuan', { nodeId: nodeId, overId: overId, position: position }, function (data) {
            me.areaTree.expandAll();
        });
    },
    getUserList: function () {
        var me = this;
        if (!this.userList) {
            this.userList = new Ext.grid.Panel({
                title: '区域用户',
                region: 'center',
                tbar: [
                    "->",
                    {
                        text: '新增成员', icon: '/Content/images/add.png', handler: function () {
                            var record = me.areaTree.getSelection()[0];
                            if (!record) {
                                Ext.Msg.alert("系统提示", "请选择一个区域");
                                return false;
                            }
                            var win = Ext.create("app.system.area.EditMember", { title: '选择区域成员', quanId: record.data.id });
                            win.on("savesuccess", function () {
                                me.userList.store.load();
                            });
                            win.show();
                        }
                    }
                ],
                viewConfig: {
                    enableTextSelection: true //可以复制单元格文字
                },
                flex: 2,
                multiColumnSort: true,
                forceFit: true,
                store: Ext.create("app.ux.PrmStore", { url: '/api/services/app/quan/GetQuanUser', autoLoad: false }),
                columns: [
                    { header: '账号', dataIndex: 'userName' },
                    { header: '姓名', dataIndex: 'name' },
                    {
                        xtype: 'actioncolumn',
                        width: 35,
                        header: localize('Operating'),
                        items: [
                        {
                            icon: '/Content/images/delete.gif',
                            tooltip: '移除',
                            handler: function (grid, rowIndex, colIndex) {
                                var record = grid.store.getAt(rowIndex);
                                Ext.Msg.confirm('系统提示', '确定要移除区域用户吗？',
                                  function (btn) {
                                      if (btn == 'yes') {
                                          App.AjaxRemove('/api/services/app/quan/RemoveQuanUser', { Id: record.data.id }, function (data) {
                                              grid.store.removeAt(rowIndex);
                                          });
                                      }
                                  });
                            }
                        }
                        ]
                    }
                ]
            });
        }
        return this.userList;
    }
});
